﻿#region Using Directives

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Web.UI;
using System.Configuration;

using QuickDev.Common;
using QuickDev.Localization;
using QuickDev.Web.UI;

using QuickDev.Model;
using QuickDev.BLL;
using QuickDev.Caching;
using QuickDev.Web.UI.Controls;
using QuickDev.Common.Utils;
using QuickDev.Common.Audit;

#endregion

namespace QuickDev.Web.App.Administration
{
    public partial class ReturnOrderEdit : AdminPage
    {
        #region Member Variables

        private int orderId = 0;
        private List<ReturnOrderHeader> listCollection;

        #endregion

        #region Page Events

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load( object sender , EventArgs e )
        {
            try
            {
                orderId = Utility.GetIntParameter( "orderId" );
                listCollection = (List<ReturnOrderHeader>)Session["ReturnOrderHeaderCollection"];

                SetProperties();

                if ( !IsPostBack )
                {
                    if ( listCollection == null || listCollection.Count == 0 )
                    {
                        btnNext.Visible = false;
                        btnPrevious.Visible = false;
                    }
                    else
                    {
                        AddRelations( listCollection );
                        btnNext.Visible = true;
                        btnPrevious.Visible = true;
                    }

                    LoadOrder( ReturnOrderHeaderCache.GetModel( orderId ) );
                }
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( ReturnOrderEdit ).Name + ".Page_Load" , ex );
                Master.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        /// <summary>
        /// Handles the Load event of the btnPrevious control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void btnPrevious_Click( object sender , EventArgs e )
        {
            try
            {
                ReturnOrderHeader model = listCollection.Find( delegate( ReturnOrderHeader itemToFind ) { return itemToFind.Id == orderId; } );
                if ( model.Previous != null && model.Previous.Id > 0 )
                {
                    Response.Redirect( string.Format( "ReturnOrderEdit.aspx?CustomerId={0}" , model.Previous.Id ) );
                }
                else
                {
                    Master.MessageCenter.DisplayInformationMessage( LocalizationUtility.GetStoreString( "BottomCustomer" ) );
                }
            }
            catch ( System.Threading.ThreadAbortException )
            {
                //ignore error.
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( ReturnOrderEdit ).Name + ".btnPrevious_Click" , ex );
                Master.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        /// <summary>
        /// Handles the Load event of the btnNext control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void btnNext_Click( object sender , EventArgs e )
        {
            try
            {
                ReturnOrderHeader model = listCollection.Find( delegate( ReturnOrderHeader itemToFind ) { return itemToFind.Id == orderId; } );
                if ( model.Next != null && model.Next.Id > 0 )
                {
                    Response.Redirect( string.Format( "ReturnOrderEdit.aspx?CustomerId={0}" , model.Next.Id ) );
                }
                else
                {
                    Master.MessageCenter.DisplayInformationMessage( LocalizationUtility.GetStoreString( "TopCustomer" ) );
                }
            }
            catch ( System.Threading.ThreadAbortException )
            {
                //ignore error.
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( ReturnOrderEdit ).Name + ".btnNext_Click" , ex );
                Master.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        #endregion

        #region Methods

        #region Protected


        #endregion

        #region Private

        /// <summary>
        /// public string RenderView( T control )
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        private string RenderView( HyperLink viewControl )
        {
            StringWriter output = new StringWriter();

            using ( HtmlTextWriter writer = new HtmlTextWriter( output ) )
            {
                viewControl.RenderControl( writer );
            }

            return output.ToString();
        }

        /// <summary>
        /// Sets the ReturnOrderEdit properties.
        /// </summary>
        private void SetProperties()
        {
            this.Title = string.Format( WebUtility.MainTitleTemplate , Master.SiteSettings.SiteName , LocalizationUtility.GetText( "titleCustomerDetail" ) );
        }

        /// <summary>
        /// LoadOrder( SalesOrderHeader model )
        /// </summary>
        /// <param name="model">model</param>
        private void LoadOrder( ReturnOrderHeader model )
        {
            lblCustomerAdministration.Text = String.Format( "<a href=\"ReturnOrderList.aspx\">{0}</a>" , "退换单列表" ) + " >> ";

            lblReturnOrderNumberDisplay.Text = model.ReturnOrderNumber;
        }

        /// <summary>
        /// Gets the next customer filtered for invisible customers.
        /// </summary>
        private ReturnOrderHeader GetNextCustomer( ReturnOrderHeader model )
        {
            if ( model.Next == null )
                return null;

            else
                return model.Next;
        }

        /// <summary>
        /// Gets the prev customer filtered for invisible customers.
        /// </summary>
        private ReturnOrderHeader GetPrevCustomer( ReturnOrderHeader model )
        {
            if ( model.Previous == null )
                return null;
            else
                return model.Previous;
        }

        /// <summary>
        /// Sets the Previous and Next properties to all customers.
        /// </summary>
        private void AddRelations( List<ReturnOrderHeader> listCollection )
        {
            for ( int i = 0 ; i < listCollection.Count ; i++ )
            {
                listCollection[i].Next = null;
                listCollection[i].Previous = null;
                if ( i > 0 )
                    listCollection[i].Next = listCollection[i - 1];

                if ( i < listCollection.Count - 1 )
                    listCollection[i].Previous = listCollection[i + 1];
            }
        }

        #endregion

        #endregion
    }
}